﻿using CoachingOrganizer.iOS.Controllers.Tools.MoneyCalculation;
using CoachingOrganizer.iOS.Decorators;
using CoachingOrganizer.iOS.Helpers;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using MonoTouch.UIKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.iOS.DataSources.Tools.MoneyCalculation
{
    public class OccasionalIncomesDataSource : UITableViewSource
    {
        public const string CellIdentifer = "OccasionalIncomesItemCell";

        private readonly UITableView _view;
        private readonly OccasionalIncomesMoneyCalculationDetailsViewController _controller;
        private readonly MoneyCalculationModel _model;
        private readonly SimpleTableDataSourceDecorator<OccasionalIncomesItemViewController, OccasionalMoneyFlowItemModel> _tableCellsSource;

        public OccasionalIncomesDataSource(UITableView view, OccasionalIncomesMoneyCalculationDetailsViewController controller, MoneyCalculationModel model)
        {
            _view = view;
            _controller = controller;
            _model = model;
            _tableCellsSource = new SimpleTableDataSourceDecorator<OccasionalIncomesItemViewController, OccasionalMoneyFlowItemModel>(CellIdentifer, _view, _controller, p => _model.Incomes.OccasionalItems.ElementAtOrDefault(p.Row), m => new OccasionalIncomesItemViewController(m));
            model.Incomes.OccasionalMoneyFlowItemsCollectionChanged += Incomes_OccasionalMoneyFlowItemsCollectionChanged;
        }

        public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            return _tableCellsSource.GetCell(indexPath);
        }

        public override int RowsInSection(UITableView tableview, int section)
        {
            return UIHelper.GetCollectionItemsCount(_model.Incomes.OccasionalItems);
        }

        private void Incomes_OccasionalMoneyFlowItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _tableCellsSource.ProceedCollectionChanged(e);
        }
    }
}
